package com.linrui.user.controller;

import com.linrui.basic.service.IVerifyCodeService;
import com.linrui.basic.utils.AjaxResult;
import com.linrui.user.dto.LoginSmsCodeDTO;
import com.linrui.user.dto.RegisterSmsCodeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @PostMapping("/sms/register")
    public AjaxResult sendRegisterSmsCode(@RequestBody RegisterSmsCodeDTO dto){

        try {
            verifyCodeService.sendRegisterSmsCode(dto);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码发送失败!"+e.getMessage());
        }
    }
    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key")String key){
        try {
            String base64ImageStr = verifyCodeService.getImageCode(key);
            return AjaxResult.me().setResult(base64ImageStr);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @PostMapping("/sms/binder")
    public AjaxResult sendBinderSmsCode(@RequestBody RegisterSmsCodeDTO dto){
        try {
            verifyCodeService.sendBinderSmsCode(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码发送失败!"+e.getMessage());
        }
    }
    @PostMapping("/sms/login")
    public AjaxResult sendLoginSmsCode(@RequestBody LoginSmsCodeDTO dto){

        try {
            verifyCodeService.sendLoginSmsCode(dto);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码发送失败!"+e.getMessage());
        }
    }
}
